<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\AuthGroupAccessApi;
use Admin\Api\AuthGroupApi;
use Admin\Api\MemberApi;
use Admin\Api\OrgMemberApi;
use Admin\Api\VUserGroupInfoApi;
use Admin\Model\OrgMemberModel;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;
use Uclient\Api\UserApi;
use Shop\Api\MemberConfigApi;

class OrgMemberController extends AdminController {

    /**
     * 用户所属机构管理
     */
    public function org(){

        $uid = $this->_param('uid',0);

        if(IS_POST){
            $province = I('post.province',array());
            $city = I('post.city',array());

            $list = array();
            if(count($province) > 0){
                foreach($province as $vo){
                    array_push($list,array(
                        'member_uid'=>$uid,
                        'type'=>OrgMemberModel::ORG_TYPE_PROVINCE,
                        'organization_id'=>$vo,
                    ));
                }
            }

            if(count($city) > 0){
                foreach($city as $vo){
                    array_push($list,array(
                        'member_uid'=>$uid,
                        'type'=>OrgMemberModel::ORG_TYPE_CITY,
                        'organization_id'=>$vo,
                    ));
                }
            }

            if(count($list) > 0){
                $reuslt = apiCall(OrgMemberApi::DELETE,array(array('member_uid'=>$uid)));
                $result = apiCall(OrgMemberApi::ADD_ALL,array($list));

                if($result['status']){
                    $this->success("添加成功!");
                }

            }else{
				$resust = apiCall(OrgMemberApi::DELETE,array(array('member_uid'=>$uid)));
				$this->success("保存成功!");

            }

        }else{


            $result = apiCall(ProvinceApi::QUERY_NO_PAGING,array());

            if($result['status']){
                $province = $result['info'];

                $result = apiCall(CityApi::QUERY_NO_PAGING,array());

                if($result['status']) {
                    $city = $result['info'];
                    $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array(array('member_uid'=>$uid)));
                    if($result['status']){
                        $org_member = $result['info'];
                    }else{
                        $org_member = array();
                    }

                    foreach($province as &$pro){
                        $pro['checked'] = 0;
                        foreach($org_member as $member){
                            if($member['organization_id'] == $pro['provinceid']){
                                $pro['checked'] = 1;
                                break;
                            }
                        }
                        foreach($city as &$vo){
                            $vo['checked'] = 0;
                            if($vo['father'] == $pro['provinceid']){

                                if(!isset($pro['_child'])){
                                    $pro['_child'] = array();
                                }

                                if($vo['city'] == "市辖区" || $vo['city'] == "县"){
                                    continue;
                                }

                                foreach($org_member as $member){
                                    if($member['organization_id'] == $vo['cityid']){
                                        $vo['checked'] = 1;
                                        $pro['checked'] = 1;
                                        break;
                                    }
                                }
                                array_push($pro['_child'],$vo);
                            }
                        }
                    }

                    $this->assign("json_area",json_encode($province));
                    $this->assign("area",$province);
                }

            }


            $this->display();
        }
    }
	public function index() {
		$params = array();
		$group_id = $this->_param('group_id',7);
		$map['nickname'] = array('like', "%" . I('nickname', '', 'trim') . "%");
        $map['group_id'] = $group_id;// array('in',array('7','8','9'));
		
		$page = array('curpage' => I('get.p'), 'size' => C('LIST_ROW'));
		$order = " last_login_time desc ";
		$params['nickname'] = I('nickname','','trim');
		$result = apiCall(VUserGroupInfoApi::QUERY, array($map, $page, $order));
		if ($result['status']) {

            $this -> assign("group_id", $group_id);
			$this -> assign("show", $result['info']['show']);
			$this -> assign("list", $result['info']['list']);
			$this -> display();
		} else {
			$this -> error($result['info']);
		}
	}
	
	/**
	 * 实名审核
	 */
	public function realname(){
		$page = array('curpage' => I('get.p'), 'size' => 15);
		$map=array('identity_validate'=>2);
		$result=apiCall(MemberConfigApi::QUERY,array($map,$page));
		$this->assign('member',$result['info']['list']);
		$resu=apiCall(MemberApi::QUERY_NO_PAGING,array());
		$this->assign('user',$resu['info']);
		$this -> display();
	}
	/**
	 * 审核通过
	 */
	public function pass(){
		$map=array('uid'=>I('id',0));
		$entity=array('identity_validate'=>1);
		$result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
		if($result['status']){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
/**
	 * 审核失败
	 */
	public function fail(){
		$map=array('uid'=>I('id',0));
		$entity=array('identity_validate'=>0);
		$result=apiCall(MemberConfigApi::SAVE,array($map,$entity));
		if($result['status']){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
	/**
	 * 删除用户
	 * 假删除
	 */
	public function delete(){
		if(is_administrator(I('uid',0))){
			$this->error("禁止对超级管理员进行删除操作！");
		}
		parent::pretendDelete("uid");
	}
	/**
	 * 启用
	 */
	public function enable(){
		parent::enable("uid");
	}
	/**
	 * 禁用
	 */
	public function disable(){
		if(is_administrator(I('uid',0))){
			$this->error("禁止对超级管理员进行禁用操作！");
		}
		parent::disable("uid");
	}
	
	/**
	 * add 
	 */
    public function add(){
		if(IS_POST){
            $group_id = I('post.group_id','');
            $username = I('post.username','');
            $password = I('post.password','');
            $repassword = I('post.repassword','');
            $email = I('post.email','');

			if($password != $repassword){
				$this->error("密码和重复密码不一致！");
			}

            if(empty($email)){
                $email = time().'@boye.com';
            }
			
			/* 调用注册接口注册用户 */
			$result = apiCall(UserApi::REGISTER, array($username, $password, $email));

            if($result['status']){ //注册成功
                $uid = $result['info'];
            	$entity = array(
					'uid'=>$uid,
					'nickname'=>$username,
					'realname'=>'',
					'idnumber'=>'',
					'add_uid'=>UID
				);
				$result = apiCall(MemberApi::ADD, array($entity));
                if(!$result['status']){
                    $this->error('用户添加失败！');
                } else {
                    if(!empty($group_id)){
                        $result = apiCall(AuthGroupAccessApi::ADD_TO_GROUP,array($uid,$group_id));
                    }
                    $this->success('用户添加成功！',U('JsjMember/index'));
                }
            } else { //注册失败，显示错误信息
                $this->error($result['info']);
            }

		}else{
			$this->display();
		}
	}


	
	/**
	 * 检测用户名是否已存在
	 */
	public function check_username($username){
		$result = apiCall(UserApi::CHECK_USER_NAME,array($username));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}
	
	 /**
	 * 检测用户名是否已存在
	 */
	public function check_email(){
			$result = apiCall(UserApi::CHECK_EMAIL,array($email));
		if($result['status']){
			echo "true";
		}else{
			echo "false";
		}
	}
	
	/**
	 * 
	 */
	public function select(){
			
		$map['nickname'] = array('like', "%" . I('q', '', 'trim') . "%");		
		$map['uid'] = I('q',-1);
		$map['_logic'] = 'OR';
		$page = array('curpage'=>0,'size'=>20);
		$order = " last_login_time desc ";
		
		$result = apiCall(MemberApi::QUERY, array($map,$page, $order,false,'uid,nickname,head'));

		if($result['status']){
			$list = $result['info']['list'];
			
			foreach($list as $key=>$g){

				$list[$key]['id']=$list[$key]['uid'];
                $list[$key]['head'] = getImageUrl($list[$key]['head']);
			}
			
			$this->success($list);
		}	
	
	}

    public function view(){
        $id = I('get.id',0);

        $result = apiCall(MemberApi::GET_INFO, array(array("uid"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userinfo",$result['info']);

        $result = apiCall(UserApi::GET_INFO, array($id));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("useraccount",$result['info']);

        $result = apiCall(AuthGroupAccessApi::QUERY_GROUP_INFO, array($id));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userroles",$result['info']);
        $this->display();
    }
}
